<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><title>C++程序开发 | Ther的小站</title><meta name="author" content="Ther"><meta name="copyright" content="Ther"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="考研复试准备时C++笔记">
<meta property="og:type" content="article">
<meta property="og:title" content="C++程序开发">
<meta property="og:url" content="http://cjx_0723.gitee.io/2023/03/18/%E5%90%8E%E7%AB%AF/C++%E5%9F%BA%E7%A1%80/C++%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91/index.html">
<meta property="og:site_name" content="Ther的小站">
<meta property="og:description" content="考研复试准备时C++笔记">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://w.wallhaven.cc/full/1p/wallhaven-1ppld1.jpg">
<meta property="article:published_time" content="2023-03-17T16:00:00.000Z">
<meta property="article:modified_time" content="2023-05-13T14:08:08.680Z">
<meta property="article:author" content="Ther">
<meta property="article:tag" content="C++基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://w.wallhaven.cc/full/1p/wallhaven-1ppld1.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://cjx_0723.gitee.io/2023/03/18/%E5%90%8E%E7%AB%AF/C++%E5%9F%BA%E7%A1%80/C++%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: {"appId":"YGMR4LH4DH","apiKey":"1fb48f3686d76eaf600890b7d3bb69c9","indexName":"index_hexo","hits":{"per_page":10},"languages":{"input_placeholder":"搜索文章","hits_empty":"找不到您查询的内容：${query}","hits_stats":"找到 ${hits} 条结果，用时 ${time} 毫秒"}},
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#2d3035","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  }
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'C++程序开发',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-05-13 22:08:08'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
    win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
      const link = document.createElement('link')
      link.rel = 'stylesheet'
      link.href = url
      if (id) link.id = id
      link.onerror = reject
      link.onload = link.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        link.onload = link.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(link)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="Ther的小站" type="application/atom+xml">
</head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">31</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">21</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://w.wallhaven.cc/full/1p/wallhaven-1ppld1.jpg')"><nav id="nav"><span id="blog-info"><a href="/" title="Ther的小站"><span class="site-name">Ther的小站</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 清单</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/Gallery/"><i class="fa-fw fas fa-images"></i><span> 照片</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">C++程序开发</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-03-17T16:00:00.000Z" title="发表于 2023-03-18 00:00:00">2023-03-18</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-05-13T14:08:08.680Z" title="更新于 2023-05-13 22:08:08">2023-05-13</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">9.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>39分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="C++程序开发"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><meta name="referrer" content="no-referrer"/>
# 变量与类型

<h3 id="1-变量的默认初始化"><a href="#1-变量的默认初始化" class="headerlink" title="1 变量的默认初始化"></a>1 变量的默认初始化</h3><h4 id="C-内置类型"><a href="#C-内置类型" class="headerlink" title="C++内置类型"></a>C++内置类型</h4><p>在C++中，如果在定义变量时没有显式地进行初始化，那么它们就会被默认初始化。默认初始化的方式取决于变量的类型和定义位置。对于内置类型的变量（如<code>int</code>、<code>float</code>、<code>double</code>等），它们的默认值是未定义的，也就是说<font color='red'><strong>它们的初始值是随机</strong></font>的。这是因为默认情况下，这些变量是在栈上分配的，它们的初始值是不确定的。</p>
<h4 id="包装类型（string）"><a href="#包装类型（string）" class="headerlink" title="包装类型（string）"></a>包装类型（string）</h4><p>对于<code>string</code>这种自定义类型，其默认初始化会<strong>调用默认构造函数来完成，即创建一个空字符串</strong>。如果在定义<code>string</code>类型的变量时没有进行显式初始化，则该变量会被默认初始化为一个空字符串。</p>
<h1 id="关键字"><a href="#关键字" class="headerlink" title="关键字"></a>关键字</h1><h2 id="1-sizeof"><a href="#1-sizeof" class="headerlink" title="1 sizeof"></a>1 sizeof</h2><h3 id="1-1-简单使用"><a href="#1-1-简单使用" class="headerlink" title="1.1 简单使用"></a>1.1 简单使用</h3><p><strong>sizeof</strong> 是一个关键字，它是一个编译时运算符，用于判断变量或数据类型的<strong>字节大小</strong>。</p>
<p>sizeof 运算符可用于获取类、结构、共用体和其他用户自定义数据类型的大小。</p>
<p>使用 sizeof 的语法如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">sizeof</span> (data type)</span><br></pre></td></tr></table></figure>

<p>其中，data type 是要计算大小的数据类型，包括类、结构、共用体和其他用户自定义数据类型。</p>
<p>请尝试下面的实例，理解 C++ 中 sizeof 的用法。复制并黏贴下面的 C++ 程序到 test.cpp 文件中，编译并运行程序。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line">using namespace <span class="built_in">std</span>;</span><br><span class="line"> </span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">   <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Size of char : &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(<span class="type">char</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">   <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Size of int : &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(<span class="type">int</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">   <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Size of short int : &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(<span class="type">short</span> <span class="type">int</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">   <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Size of long int : &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(<span class="type">long</span> <span class="type">int</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">   <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Size of float : &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(<span class="type">float</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">   <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Size of double : &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(<span class="type">double</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">   <span class="built_in">cout</span> &lt;&lt; <span class="string">&quot;Size of wchar_t : &quot;</span> &lt;&lt; <span class="keyword">sizeof</span>(<span class="type">wchar_t</span>) &lt;&lt; <span class="built_in">endl</span>;</span><br><span class="line">   <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>当上面的代码被编译和执行时，它会产生下列结果，结果会根据使用的机器而不同：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Size of <span class="type">char</span> : <span class="number">1</span></span><br><span class="line">Size of <span class="type">int</span> : <span class="number">4</span></span><br><span class="line">Size of <span class="type">short</span> <span class="type">int</span> : <span class="number">2</span></span><br><span class="line">Size of <span class="type">long</span> <span class="type">int</span> : <span class="number">4</span></span><br><span class="line">Size of <span class="type">float</span> : <span class="number">4</span></span><br><span class="line">Size of <span class="type">double</span> : <span class="number">8</span></span><br><span class="line">Size of <span class="type">wchar_t</span> : <span class="number">4</span></span><br></pre></td></tr></table></figure>

<h3 id="1-2-sizeof计算数组长度"><a href="#1-2-sizeof计算数组长度" class="headerlink" title="1.2 sizeof计算数组长度"></a>1.2 sizeof计算数组长度</h3><p> 在确定一个数组大小的时候我们一般用sizeof</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> arr1[] = &#123;<span class="number">1</span> , <span class="number">2</span>, <span class="number">4</span> ,<span class="number">5</span>&#125;;</span><br><span class="line"><span class="type">int</span> arr1_len = <span class="keyword">sizeof</span> arr1/<span class="built_in">sizeof</span>(<span class="type">int</span>);</span><br><span class="line">std::cout&lt;&lt; <span class="string">&quot;arr1 len: &quot;</span> &lt;&lt; arr1_len &lt;&lt; std::endl; <span class="comment">// 输出4</span></span><br></pre></td></tr></table></figure>

<hr>
<h2 id="2-malloc"><a href="#2-malloc" class="headerlink" title="2 malloc"></a>2 malloc</h2><h3 id="2-1-malloc函数"><a href="#2-1-malloc函数" class="headerlink" title="2.1 malloc函数"></a>2.1 malloc函数</h3><p>malloc的全称是memory allocation，中文叫动态内存分配。<br><strong>用于申请一块连续的指定大小的内存块区域以void*类型返回分配的内存区域地址，当无法知道内存具体位置的时候，想要绑定真正的内存空间，就需要用到动态的分配内存，且分配的大小就是程序要求的大小。</strong></p>
<p>malloc() 在**<font color='red'>堆区</font>**分配一块指定大小的内存空间，用来存放数据。这块内存空间在函数执行完成后不会被初始化，它们的值是未知的。如果希望在分配内存的同时进行初始化，请使用 calloc() 函数。</p>
<p>malloc函数原型：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">extern</span> <span class="type">void</span> *<span class="title">malloc</span><span class="params">(<span class="type">unsigned</span> <span class="type">int</span> num_bytes)</span></span>;  <span class="comment">// 意为分配长度为num_bytes字节的内存块</span></span><br></pre></td></tr></table></figure>

<p>malloc函数头文件：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;malloc.h&gt;</span></span></span><br></pre></td></tr></table></figure>

<p>malloc函数返回值：</p>
<p>如果分配成功则返回指向被分配内存的指针，否则返回空指针NULL。</p>
<p>malloc函数使用注意事项：</p>
<p>malloc函数的返回的是无类型指针，在使用时一定要强制转换为所需要的类型。<br><strong>（敲黑板）重点：在使用malloc开辟空间时，使用完成一定要释放空间，如果不释放会造内存泄漏。</strong><br><strong>在使用malloc函数开辟的空间中，不要进行指针的移动，因为一旦移动之后可能出现申请的空间和释放空间大小的不匹配</strong></p>
<h3 id="2-2-malloc函数与动态数组"><a href="#2-2-malloc函数与动态数组" class="headerlink" title="2.2 malloc函数与动态数组"></a>2.2 malloc函数与动态数组</h3><p>关于malloc所开辟空间类型：malloc只开辟空间，不进行类型检查，只是在使用的时候进行类型的强转</p>
<p>指针自身 &#x3D; (指针类型）malloc（sizeof（指针类型） * 数据数量）</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> *p = <span class="literal">NULL</span>;</span><br><span class="line"><span class="type">int</span> n = <span class="number">10</span>;</span><br><span class="line">p = (<span class="type">int</span> *)<span class="built_in">malloc</span>(<span class="keyword">sizeof</span>(<span class="type">int</span>)*n);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 操作数组</span></span><br><span class="line">p[<span class="number">0</span>] = <span class="number">10</span>;</span><br><span class="line"><span class="built_in">cout</span> &lt;&lt; p[<span class="number">0</span>];</span><br></pre></td></tr></table></figure>

<p>在使用malloc函数之前我们一定要<strong>计算字节数</strong>，malloc开辟的是用户所需求的字节数大小的空间。</p>
<h3 id="2-3-malloc函数与二维动态数组"><a href="#2-3-malloc函数与二维动态数组" class="headerlink" title="2.3 malloc函数与二维动态数组"></a>2.3 malloc函数与二维动态数组</h3><p>方法一：利用二级指针申请一个二维数组。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span>  </span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span>  </span></span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span>  </span></span><br><span class="line"><span class="function"></span>&#123;  </span><br><span class="line">    <span class="type">int</span> **a;  <span class="comment">//用二级指针动态申请二维数组  </span></span><br><span class="line">    <span class="type">int</span> i,j;  </span><br><span class="line">    <span class="type">int</span> m,n;  </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;请输入行数\n&quot;</span>);  </span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;m);  </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;请输入列数\n&quot;</span>);  </span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>,&amp;n);  </span><br><span class="line">    a=(<span class="type">int</span>**)<span class="built_in">malloc</span>(<span class="built_in">sizeof</span>(<span class="type">int</span>*)*m);  </span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;m;i++)  </span><br><span class="line">    a[i]=(<span class="type">int</span>*)<span class="built_in">malloc</span>(<span class="built_in">sizeof</span>(<span class="type">int</span>)*n);  </span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;m;i++)  </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span>(j=<span class="number">0</span>;j&lt;n;j++)  </span><br><span class="line">        &#123;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%p\n&quot;</span>,&amp;a[i][j]);     <span class="comment">//输出每个元素地址，每行的列与列之间的地址时连续的，行与行之间的地址不连续</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;m;i++)  </span><br><span class="line">    <span class="built_in">free</span>(a[i]);</span><br><span class="line"> </span><br><span class="line">    <span class="built_in">free</span>(a);  </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;  </span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<p><strong>方法二：用数组指针形式申请一个二维数组。</strong></p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span>  </span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdlib.h&gt;</span>  </span></span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span>  </span></span><br><span class="line"><span class="function"></span>&#123;  </span><br><span class="line">    <span class="type">int</span> i,j;  </span><br><span class="line">    <span class="comment">//申请一个3行2列的整型数组  </span></span><br><span class="line">    <span class="built_in">int</span> (*a)[<span class="number">2</span>]=(<span class="built_in">int</span>(*)[<span class="number">2</span>])<span class="built_in">malloc</span>(<span class="built_in">sizeof</span>(<span class="type">int</span>)*<span class="number">3</span>*<span class="number">2</span>);  </span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)  </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span>(j=<span class="number">0</span>;j&lt;<span class="number">2</span>;j++)  </span><br><span class="line">        &#123;  </span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%p\n&quot;</span>,&amp;a[i][j]);  <span class="comment">//输出数组每个元素地址，每个元素的地址是连续的</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line">    <span class="built_in">free</span>(a);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;  </span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>

<p>方法三：用一个单独的一维数组来模拟二维数组。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> nrows,ncolumns;</span><br><span class="line">    <span class="type">int</span> *Array;</span><br><span class="line">    <span class="type">int</span> i,j;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;please input nrows&amp;ncolumns:\n&quot;</span>);</span><br><span class="line">    <span class="built_in">scanf</span>(<span class="string">&quot;%d%d&quot;</span>,&amp;nrows,&amp;ncolumns);</span><br><span class="line">    Array=(<span class="type">int</span> *)<span class="built_in">malloc</span>(nrows*ncolumns*<span class="built_in">sizeof</span>(<span class="type">int</span>));   <span class="comment">//申请内存空间</span></span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>;i&lt;nrows;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span>(j=<span class="number">0</span>;j&lt;ncolumns;j++)</span><br><span class="line">        &#123;</span><br><span class="line">            Array[i*ncolumns+j]=<span class="number">1</span>;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>,Array[i*ncolumns+j]);   <span class="comment">//用Array[i*ncolumns+j] 访问第i,j个成员</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">free</span>(Array);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"> </span><br></pre></td></tr></table></figure>

<h2 id="3-define-与-typedef"><a href="#3-define-与-typedef" class="headerlink" title="3 define 与 typedef"></a>3 define 与 typedef</h2><h3 id="3-1-define"><a href="#3-1-define" class="headerlink" title="3.1 define"></a>3.1 define</h3><p><code>#DEFINE</code>关键字在C&#x2F;C++中用作宏处理，基本的用法为：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">#DEFINE MAX_SIZE <span class="number">256</span></span><br></pre></td></tr></table></figure>

<p>在代码的预处理阶段，编译器会把所有的宏定义符号替换成被定义的数据，例如以下代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="comment">// 注意不用加分号!</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> M (a + b)</span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> MUL(x,y) x*y</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> a = <span class="number">2</span>;</span><br><span class="line">	<span class="type">int</span> b = <span class="number">2</span>;</span><br><span class="line">	cout &lt;&lt; M * M;  <span class="comment">//16</span></span><br><span class="line">     cout &lt;&lt; <span class="built_in">MUL</span>(a,b); <span class="comment">//4</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-2-typedef"><a href="#3-2-typedef" class="headerlink" title="3.2 typedef"></a>3.2 typedef</h3><h2 id="4-memset"><a href="#4-memset" class="headerlink" title="4 memset"></a>4 memset</h2><h3 id="4-1-memset使用"><a href="#4-1-memset使用" class="headerlink" title="4.1 memset使用"></a>4.1 memset使用</h3><p>memset函数定义于&lt;string.h&gt;头文件中。<br>函数原型：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> *<span class="title">memset</span><span class="params">(<span class="type">void</span> *s,<span class="type">int</span> c,<span class="type">unsigned</span> <span class="type">long</span> n)</span></span>;</span><br></pre></td></tr></table></figure>

<p>函数功能：为指针变量s所指的前n个<strong>字节</strong>的内存单元填充给定的int型数值c，它可以为任何数据进行初始化。<strong>换句话说，就是将数值c以单个字节逐个拷贝的方式放到指针变量s所指的内存中去。</strong> 注意：只将数值c的最低一个字节填充到内存。</p>
<p>示例：<br>（1）当c&#x3D;-1时</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> dp[<span class="number">3</span>];</span><br><span class="line">    <span class="built_in">memset</span>(dp,<span class="number">-1</span>,<span class="built_in">sizeof</span>(dp));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)</span><br><span class="line">        cout &lt;&lt; dp[i] &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>因为-1在计算机中存储为：1111 1111，故dp数组中每一个int值为“1111 1111 1111 1111 1111 1111 1111 1111”，是十进制下的-1。<br>输出结果：-1 -1 -1</p>
</blockquote>
<p>（2）当c&#x3D;0时</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> dp[<span class="number">3</span>];</span><br><span class="line">    <span class="built_in">memset</span>(dp,<span class="number">0</span>,<span class="built_in">sizeof</span>(dp));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)</span><br><span class="line">        cout &lt;&lt; dp[i] &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>因为0在计算机中存储为：0000 0000，故dp数组中每一个int值为“0000 0000 0000 0000 0000 0000 0000 0000”，是十进制下的0。<br>输出结果：0 0 0</p>
</blockquote>
<p>（3）当c&#x3D;1时</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> dp[<span class="number">3</span>];</span><br><span class="line">    <span class="built_in">memset</span>(dp,<span class="number">1</span>,<span class="built_in">sizeof</span>(dp));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)</span><br><span class="line">        cout &lt;&lt; dp[i] &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>因为1在计算机中存储为：0000 0001，故dp数组中每一个int值为“0000 0001 0000 0001 0000 0001 0000 0001”，是十进制下的16843009。<br>输出结果：16843009 16843009 16843009</p>
</blockquote>
<p>（4）当c&#x3D;268时</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> dp[<span class="number">3</span>];</span><br><span class="line">    <span class="built_in">memset</span>(dp,<span class="number">268</span>,<span class="built_in">sizeof</span>(dp));</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)</span><br><span class="line">        cout &lt;&lt; dp[i] &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>因为-1在计算机中存储为： 0001 0000 1100，故dp数组中每一个int值为“0000 1100 0000 1100 0000 1100 0000 1100”，是十进制下的202116108。<br>输出结果：202116108 202116108 202116108</p>
</blockquote>
<h3 id="4-2-memset与动态数组初始化"><a href="#4-2-memset与动态数组初始化" class="headerlink" title="4.2 memset与动态数组初始化"></a>4.2 memset与动态数组初始化</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">bool</span>* used = (<span class="type">bool</span>*)<span class="built_in">malloc</span>(<span class="built_in">sizeof</span>(<span class="type">bool</span>) * (n + <span class="number">1</span>));</span><br><span class="line"><span class="built_in">memset</span>(used, <span class="number">0</span>, <span class="built_in">sizeof</span>(used));</span><br></pre></td></tr></table></figure>

<p>上述代码初始化 used 数组会失败，原因是 sizeof(used) 返回的是 used 指针的大小，不能返回指针所指数组的长度大小</p>
<h2 id="5-union"><a href="#5-union" class="headerlink" title="5 union"></a>5 union</h2><blockquote>
<p>在C++中，union是一种用户自定义数据类型，用于存储不同类型的数据。union中的所有成员共享同一段内存空间，即它们在内存中占用的是同一个位置。union的大小等于最大成员的大小。</p>
<p>union有以下特点和用途：</p>
<ol>
<li>节省内存：由于union中的所有成员共享同一段内存空间，因此可以节省内存，特别是当需要存储的数据类型不确定时。</li>
<li>用于存储不同类型的数据：可以使用union来存储不同类型的数据，例如可以用联合体来实现一个变量同时存储整数、浮点数等不同类型的数据。</li>
<li>用于类型转换：由于union中的成员共享同一段内存空间，因此可以使用联合体来进行类型转换，即通过一个成员访问另一个成员的数据。</li>
</ol>
</blockquote>
<p>以下是一个简单的使用union的例子：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">union</span> <span class="title class_">MyUnion</span> &#123;</span><br><span class="line">    <span class="type">int</span> a;</span><br><span class="line">    <span class="type">float</span> b;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    MyUnion u;</span><br><span class="line">    u.a = <span class="number">42</span>;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;a = &quot;</span> &lt;&lt; u.a &lt;&lt; endl; <span class="comment">// 输出 42</span></span><br><span class="line">    u.b = <span class="number">3.14</span>;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;b = &quot;</span> &lt;&lt; u.b &lt;&lt; endl; <span class="comment">// 输出 3.14</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;a = &quot;</span> &lt;&lt; u.a &lt;&lt; endl; <span class="comment">// 输出不确定的值，因为b和a共享同一段内存空间</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>需要注意的是，由于union的成员共享同一段内存空间，因此对一个成员的修改可能会影响到其他成员的值，因此在使用union时需要特别小心。</p>
<h1 id="知识点"><a href="#知识点" class="headerlink" title="知识点"></a>知识点</h1><h2 id="1-堆和栈"><a href="#1-堆和栈" class="headerlink" title="1 堆和栈"></a>1 堆和栈</h2><p>堆和栈主要有以下几点不同：</p>
<h3 id="申请方式"><a href="#申请方式" class="headerlink" title="申请方式"></a><strong>申请方式</strong></h3><p>栈：<strong>申请栈空间时不需要指明大小</strong>。例如声明在函数中一个局部变量<code>int b</code>; 系统自动在栈中为b开辟空间。</p>
<p>堆：<strong>申请堆空间时需要指明大小</strong>。例如，C中的<a target="_blank" rel="noopener" href="https://so.csdn.net/so/search?q=malloc%E5%87%BD%E6%95%B0&spm=1001.2101.3001.7020">malloc函数</a><code>p1 = (char *)malloc(10)</code>。C++中的new运算符<code>p2 = new char[10]</code>。<font color='red'>但是p1、p2本身是在栈中的</font>。</p>
<h3 id="分配方式"><a href="#分配方式" class="headerlink" title="分配方式"></a><strong>分配方式</strong></h3><p>栈：<strong>栈是向低地址扩展的数据结构，是一块连续的内存的区域</strong>。只要栈的剩余空间大于所申请空间，系统将为程序提供内存，否则将报异常提示栈溢出。</p>
<p>堆：<strong>堆是向高地址扩展的数据结构，是不连续的内存区域</strong>。操作系统有一个记录空闲内存地址的链表，当系统收到malloc申请时，会遍历该链表，寻找第一个空间大于所申请空间的堆结点，然后将该结点从空闲结点链表中删除，并将该结点的空间分配给程序，另外，对于大多数系统，会在这块内存空间中的首地址处记录本次分配的大小，这样，代码中的delete语句才能正确的释放本内存空间。另外，由于找到的堆结点的大小不一定正好等于申请的大小，系统会自动的将多余的那部分重新放入空闲链表中。</p>
<h3 id="空间大小"><a href="#空间大小" class="headerlink" title="空间大小"></a><strong>空间大小</strong></h3><p>栈：linux下默认的栈空间大小是8M或10M。</p>
<p>堆：堆的大小受限于计算机系统中有效的虚拟内存（虚拟内存中还有一部分空间要留给内核）。针对 Linux 操作系统而言，<strong>最高的1G 字节</strong>(从虚拟地址 0xC0000000 到 0xFFFFFFFF)由内核使用，称为内核空间。而<strong>较低的 3G 字节</strong>(从虚拟地址 0x00000000 到 0xBFFFFFFF)由各个进程使用，称为用户空间。</p>
<h3 id="存储内容"><a href="#存储内容" class="headerlink" title="存储内容"></a><strong>存储内容</strong></h3><p>栈：<strong>存放局部变量、函数参数</strong>。存放在栈中的数据只在当前函数及下一层函数中有效，一旦函数返回了，这些数据也就自动释放了。</p>
<p>堆：堆中的具体内容可以通过程序员安排。</p>
<h2 id="2-new-x2F-delete和malloc-x2F-free"><a href="#2-new-x2F-delete和malloc-x2F-free" class="headerlink" title="2 new&#x2F;delete和malloc&#x2F;free"></a>2 new&#x2F;delete和malloc&#x2F;free</h2><h3 id="2-1-区别"><a href="#2-1-区别" class="headerlink" title="2.1 区别"></a>2.1 区别</h3><ol>
<li>new从自由存储区上分配内存，malloc从堆上分配内存。自由存储区是C++基于new操作符的一个抽象概念，凡是通过new操作符进行内存申请，该内存即为自由存储区。而堆是操作系统中的术语，是操作系统所维护的一块特殊内存，用于程序的内存动态分配。自由存储区是否能够是堆取决于operator new 的实现细节。自由存储区不仅可以是堆，还可以是静态存储区，这都看operator new在哪里为对象分配内存。</li>
<li>new 可以调用对象的构造函数，对应的 delete 调用相应的析构函数；malloc 仅仅分配内存，free 仅仅回收内存，并不执行构造和析构函数。在new一个对象的时候，底层首先调用 operator new() 函数为对象分配内存空间，然后调用对象的构造函数。delete会调用对象的析构函数，然后调用free回收内存。</li>
<li>使用new操作符申请内存分配时无须指定内存块的大小，编译器会根据类型信息自行计算；使用malloc则需要显式地指出所需内存的尺寸。</li>
<li>new、delete 返回的是某种数据类型指针；malloc、free 返回的是 void 指针。</li>
<li>new、delete 是操作符；malloc、free 是函数。</li>
<li>malloc分配失败返回NULL；new要求在内存分配失败时要求返回NULL或抛出std::bad_alloc异常。</li>
</ol>
<h3 id="2-2-malloc的内存可以用delete释放吗？"><a href="#2-2-malloc的内存可以用delete释放吗？" class="headerlink" title="2.2 malloc的内存可以用delete释放吗？"></a>2.2 malloc的内存可以用delete释放吗？</h3><p>可以，但是一般不这么用。malloc&#x2F;free是c语言中的函数，c++为了兼容c保留下来这一对函数。简单来说，new 可以理解为，先执行malloc来申请内存，后调用构造函数来初始化对象；delete是先执行析构函数，后使用free来释放内存。delete[] 用于释放数组元素的分配。</p>
<h2 id="3-运算符重载"><a href="#3-运算符重载" class="headerlink" title="3 运算符重载"></a>3 运算符重载</h2><h3 id="示例-1：一元运算符重载"><a href="#示例-1：一元运算符重载" class="headerlink" title="示例 1：一元运算符重载"></a>示例 1：一元运算符重载</h3><p>一元运算符即只对一个操作数进行操作的运算符，例如：<code>!obj</code>、<code>-obj</code>、<code>++obj</code> 、<code>obj++</code> 或 <code>obj--</code> 等等。</p>
<p>下面示例将对负号（<code>-</code>）进行重载：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Distance</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">int</span> feet;    <span class="comment">// 0 到无穷</span></span><br><span class="line">    <span class="type">int</span> inches;  <span class="comment">// 0 到 12</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">// 构造函数</span></span><br><span class="line">    <span class="built_in">Distance</span>() &#123;</span><br><span class="line">        feet = <span class="number">0</span>;</span><br><span class="line">        inches = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="built_in">Distance</span>(<span class="type">int</span> f, <span class="type">int</span> i) &#123;</span><br><span class="line">        feet = f;</span><br><span class="line">        inches = i;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 显示距离</span></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">displayDistance</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;F: &quot;</span> &lt;&lt; feet &lt;&lt; <span class="string">&quot;, I: &quot;</span> &lt;&lt; inches &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 重载负运算符 ( - )</span></span><br><span class="line">    Distance <span class="keyword">operator</span>- () &#123;</span><br><span class="line">        feet = -feet;</span><br><span class="line">        inches = -inches;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">Distance</span>(feet, inches);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Distance <span class="title">d1</span><span class="params">(<span class="number">1</span>, <span class="number">10</span>)</span>, <span class="title">d2</span><span class="params">(<span class="number">-5</span>, <span class="number">110</span>)</span></span>;</span><br><span class="line">    </span><br><span class="line">    -d1;                     <span class="comment">// 取相反数</span></span><br><span class="line">    d1.<span class="built_in">displayDistance</span>();    <span class="comment">// 距离 D1</span></span><br><span class="line">    </span><br><span class="line">    -d2;                     <span class="comment">// 取相反数</span></span><br><span class="line">    d2.<span class="built_in">displayDistance</span>();    <span class="comment">// 距离 D2</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译并运行以上示例，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">F: -1, I: -10</span><br><span class="line">F: 5, I: -110</span><br><span class="line">12</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="示例-2：二元运算符重载"><a href="#示例-2：二元运算符重载" class="headerlink" title="示例 2：二元运算符重载"></a>示例 2：二元运算符重载</h3><p>二元运算符即需要两个参数的运算符，例如：加运算符（<code>+</code>）、减运算符（<code>-</code>）、乘运算符（<code>*</code>）、除运算符（<code>/</code>）。</p>
<p>下面示例将重载加运算符（<code>+</code>）：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Box</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">double</span> length;  <span class="comment">// 长度</span></span><br><span class="line">    <span class="type">double</span> width;   <span class="comment">// 宽度</span></span><br><span class="line">    <span class="type">double</span> height;  <span class="comment">// 高度</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Box</span>() &#123;</span><br><span class="line">        length = <span class="number">0.0</span>;</span><br><span class="line">        width = <span class="number">0.0</span>;</span><br><span class="line">        height = <span class="number">0.0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">Box</span>(<span class="type">double</span> a, <span class="type">double</span> b, <span class="type">double</span> c)</span><br><span class="line">    &#123;</span><br><span class="line">        length = a;</span><br><span class="line">        width = b;</span><br><span class="line">        height = c;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">double</span> <span class="title">getVolume</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> length * width * height;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 重载 + 运算符，用于把两个 Box 对象相加</span></span><br><span class="line">    Box <span class="keyword">operator</span>+(<span class="type">const</span> Box&amp; b)</span><br><span class="line">    &#123;</span><br><span class="line">        Box box;</span><br><span class="line">        box.length = <span class="keyword">this</span>-&gt;length + b.length;</span><br><span class="line">        box.width = <span class="keyword">this</span>-&gt;width + b.width;</span><br><span class="line">        box.height = <span class="keyword">this</span>-&gt;height + b.height;</span><br><span class="line">        <span class="keyword">return</span> box;</span><br><span class="line">    &#125;</span><br><span class="line">     </span><br><span class="line">    <span class="comment">// 友元重载法：</span></span><br><span class="line">    <span class="comment">//friend Box operator+(const Box&amp; box1, const Box&amp; box2) &#123;</span></span><br><span class="line">    <span class="comment">//    Box box;</span></span><br><span class="line">    <span class="comment">//    box.height = box1.height + box2.height;</span></span><br><span class="line">    <span class="comment">//    box.length = box1.length + box2.length;</span></span><br><span class="line">    <span class="comment">//    box.width = box1.width + box2.width;</span></span><br><span class="line">    <span class="comment">//    return box;</span></span><br><span class="line">    <span class="comment">//&#125;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Box <span class="title">b1</span><span class="params">(<span class="number">5.0</span>, <span class="number">4.0</span>, <span class="number">3.0</span>)</span></span>;</span><br><span class="line">    <span class="function">Box <span class="title">b2</span><span class="params">(<span class="number">6.0</span>, <span class="number">5.0</span>, <span class="number">4.0</span>)</span></span>;</span><br><span class="line">    Box b3;</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;Volume of b1 : &quot;</span> &lt;&lt; b1.<span class="built_in">getVolume</span>() &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;Volume of b2 : &quot;</span> &lt;&lt; b2.<span class="built_in">getVolume</span>() &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 把两个对象相加，得到 Box3</span></span><br><span class="line">    b3 = b1 + b2;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// Box3 的体积</span></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;Volume of b3 : &quot;</span> &lt;&lt; b3.<span class="built_in">getVolume</span>() &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>使用 <code>g++ main.cpp &amp;&amp; ./a.out</code> 命令编译运行以上示例，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Volume of b1 : 60</span><br><span class="line">Volume of b2 : 120</span><br><span class="line">Volume of b3 : 693</span><br><span class="line">123</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="示例-3：关系运算符重载"><a href="#示例-3：关系运算符重载" class="headerlink" title="示例 3：关系运算符重载"></a>示例 3：<a target="_blank" rel="noopener" href="https://so.csdn.net/so/search?q=%E5%85%B3%E7%B3%BB%E8%BF%90%E7%AE%97%E7%AC%A6&spm=1001.2101.3001.7020">关系运算符</a>重载</h3><p>C++ 允许重载任何一个关系运算符（例如 &lt; 、 &gt; 、 &lt;&#x3D; 、 &gt;&#x3D; 、 &#x3D;&#x3D; 等），重载后的关系运算符可用于比较类的对象。许多 C++ 内置的数据类型也都支持各种关系运算符。</p>
<p>下面示例将重载小于符（&lt;）：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Rect</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">double</span> width;</span><br><span class="line">    <span class="type">double</span> height;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Rect</span>(<span class="type">double</span> a, <span class="type">double</span> b)</span><br><span class="line">    &#123;</span><br><span class="line">        width = a;</span><br><span class="line">        height = b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">double</span> <span class="title">area</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> width * height;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 重载小于运算符 ( &lt; ), 按照面积比大小</span></span><br><span class="line">    <span class="type">bool</span> <span class="keyword">operator</span>&lt;(Rect&amp; that)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>-&gt;<span class="built_in">area</span>() &lt; that.<span class="built_in">area</span>();</span><br><span class="line">    &#125;</span><br><span class="line">     </span><br><span class="line">    <span class="comment">// 友元方式</span></span><br><span class="line">    <span class="comment">//friend bool operator&lt;(Rect&amp; r1,  Rect&amp; r2) &#123;</span></span><br><span class="line">    <span class="comment">//    return r1.area() &lt; r2.area();</span></span><br><span class="line">    <span class="comment">//&#125;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Rect <span class="title">r1</span><span class="params">(<span class="number">3.0</span>, <span class="number">5.0</span>)</span>, <span class="title">r2</span><span class="params">(<span class="number">3.5</span>, <span class="number">4.5</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;Area of r1 = &quot;</span> &lt;&lt; r1.<span class="built_in">area</span>() &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;Area of r2 = &quot;</span> &lt;&lt; r2.<span class="built_in">area</span>() &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (r1 &lt; r2)</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;r1 is less than r2&quot;</span> &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;r1 is large than r2&quot;</span> &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译运行以上代码，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Area of r1 = 15</span><br><span class="line">Area of r2 = 15.75</span><br><span class="line">r1 is less than r2</span><br><span class="line">123</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="示例-4：输入-x2F-输出运算符重载"><a href="#示例-4：输入-x2F-输出运算符重载" class="headerlink" title="示例 4：输入&#x2F;输出运算符重载"></a>示例 4：输入&#x2F;输出运算符重载</h3><p>C++ 使用<strong>流提取运算符</strong>（<code>&gt;&gt;</code>）和<strong>流插入运算符</strong>（<code>&lt;&lt;</code>）来输入和输出内置的数据类型，同时也允许重载 <code>&gt;&gt;</code> 和 <code>&lt;&lt;</code> 来操作对象等用户自定义的数据类型。</p>
<p>可以把运算符重载函数声明为类的友元函数，这样就可以不用创建对象而直接调用函数。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Rect</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="type">double</span> width;</span><br><span class="line">    <span class="type">double</span> height;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">Rect</span>() &#123;</span><br><span class="line">        width = <span class="number">0</span>;</span><br><span class="line">        height = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">Rect</span>(<span class="type">double</span> a, <span class="type">double</span> b )</span><br><span class="line">    &#123;</span><br><span class="line">        width  = a;</span><br><span class="line">        height = b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">double</span> <span class="title">area</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> width * height;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">friend</span> std::ostream &amp;<span class="keyword">operator</span>&lt;&lt;(std::ostream &amp;output, Rect &amp;r)</span><br><span class="line">    &#123; </span><br><span class="line">        output &lt;&lt; <span class="string">&quot;width: &quot;</span> &lt;&lt; r.width &lt;&lt; <span class="string">&quot;, &quot;</span>;</span><br><span class="line">        output &lt;&lt; <span class="string">&quot;height: &quot;</span> &lt;&lt; r.height &lt;&lt; <span class="string">&quot;, &quot;</span>;</span><br><span class="line">        output &lt;&lt; <span class="string">&quot;area: &quot;</span> &lt;&lt; r.<span class="built_in">area</span>();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> output;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">friend</span> std::istream &amp;<span class="keyword">operator</span>&gt;&gt;(std::istream &amp;input, Rect &amp;r)</span><br><span class="line">    &#123;</span><br><span class="line">        input &gt;&gt; r.width &gt;&gt; r.height;</span><br><span class="line">        <span class="keyword">return</span> input;            </span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Rect <span class="title">r1</span><span class="params">(<span class="number">3.0</span>, <span class="number">4.0</span>)</span>, <span class="title">r2</span><span class="params">(<span class="number">6.0</span>, <span class="number">8.0</span>)</span>, r3</span>;</span><br><span class="line">    </span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;Enter the value of object: \n&quot;</span>;</span><br><span class="line">    cin &gt;&gt; r3;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;r1: &quot;</span> &lt;&lt; r1 &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;r2: &quot;</span> &lt;&lt; r2 &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;r3: &quot;</span> &lt;&lt; r3 &lt;&lt; endl;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译和运行以上示例，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Enter the value of object: </span><br><span class="line">2 3</span><br><span class="line">r1: width: 3, height: 4, area: 12</span><br><span class="line">r2: width: 6, height: 8, area: 48</span><br><span class="line">r3: width: 2, height: 3, area: 6</span><br><span class="line">12345</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="示例-5：-和-–-运算符重载"><a href="#示例-5：-和-–-运算符重载" class="headerlink" title="示例 5：++ 和 – 运算符重载"></a>示例 5：++ 和 – 运算符重载</h3><p>递增运算符（<code>++</code>）和递减运算符（<code>--</code>）是 C++ 语言中两个重要的一元运算符，包括前缀和后缀两种用法。</p>
<p>下面示例将演示如何重载前缀自增（<code>++obj</code>）和后缀自减运算符（<code>obj--</code>）：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Rect</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">double</span> width;</span><br><span class="line">    <span class="type">double</span> height;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Rect</span>(<span class="type">double</span> a, <span class="type">double</span> b)</span><br><span class="line">    &#123;</span><br><span class="line">        width = a;</span><br><span class="line">        height = b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">double</span> <span class="title">area</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> width * height;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 重载小于运算符 ( &lt; ), 按照面积比大小</span></span><br><span class="line">    <span class="type">bool</span> <span class="keyword">operator</span>&lt;(Rect&amp; that)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">this</span>-&gt;<span class="built_in">area</span>() &lt; that.<span class="built_in">area</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 友元方式</span></span><br><span class="line">    <span class="comment">//friend bool operator&lt;(Rect&amp; r1,  Rect&amp; r2) &#123;</span></span><br><span class="line">    <span class="comment">//    return r1.area() &lt; r2.area();</span></span><br><span class="line">    <span class="comment">//&#125;</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Rect <span class="title">r1</span><span class="params">(<span class="number">3.0</span>, <span class="number">5.0</span>)</span>, <span class="title">r2</span><span class="params">(<span class="number">3.5</span>, <span class="number">4.5</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;Area of r1 = &quot;</span> &lt;&lt; r1.<span class="built_in">area</span>() &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;Area of r2 = &quot;</span> &lt;&lt; r2.<span class="built_in">area</span>() &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (r1 &lt; r2)</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;r1 is less than r2&quot;</span> &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;r1 is large than r2&quot;</span> &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译运行以上示例，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">12 : 58</span><br><span class="line">12 : 59</span><br><span class="line">13 : 0</span><br><span class="line">0 : 45</span><br><span class="line">0 : 45</span><br><span class="line">0 : 46</span><br><span class="line">123456</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="示例-6：赋值运算符重载"><a href="#示例-6：赋值运算符重载" class="headerlink" title="示例 6：赋值运算符重载"></a>示例 6：赋值运算符重载</h3><p>C++ 允许重载赋值运算符（<code>=</code>），用于创建一个对象，比如拷贝构造函数。</p>
<p>下面示例重载 <code>Rect</code> 中的赋值运算符，并在每次拷贝的时候就把长度和宽度数值各加 1。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Rect</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">double</span> width;</span><br><span class="line">    <span class="type">double</span> height;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Rect</span>() &#123;</span><br><span class="line">        width = <span class="number">0</span>;</span><br><span class="line">        height = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">Rect</span>(<span class="type">double</span> a, <span class="type">double</span> b) &#123;</span><br><span class="line">        width = a;</span><br><span class="line">        height = b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">display</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot; width: &quot;</span> &lt;&lt; width;</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot; height: &quot;</span> &lt;&lt; height;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="type">void</span> <span class="keyword">operator</span>= (<span class="type">const</span> Rect &amp;r)</span><br><span class="line">    &#123;</span><br><span class="line">        width = r.width + <span class="number">1</span>;</span><br><span class="line">        height = r.height + <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Rect <span class="title">r1</span><span class="params">(<span class="number">3.0</span>, <span class="number">4.0</span>)</span>, r2</span>;</span><br><span class="line">    </span><br><span class="line">    r2 = r1;</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;r1: &quot;</span>;</span><br><span class="line">    r1.<span class="built_in">display</span>();</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;r2: &quot;</span>;</span><br><span class="line">    r2.<span class="built_in">display</span>();</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="number">123456789101112131415161718192021222324252627282930313233343536373839404142434445464748</span></span><br></pre></td></tr></table></figure>

<p>编译和运行以上示例，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">r1:  width: 3 height: 4</span><br><span class="line">r2:  width: 4 height: 5</span><br><span class="line">12</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="示例-7：函数调用运算符重载"><a href="#示例-7：函数调用运算符重载" class="headerlink" title="示例 7：函数调用运算符重载"></a>示例 7：函数调用运算符重载</h3><p>C++ 允许重载函数调用运算符（即 <code>()</code> 符号）。重载 <code>()</code> 的目的不是为了创造一种新的调用函数的方式，而是创建一个可以传递任意个参数的运算符函数。其实就是创建一个可调用的对象。</p>
<p>下面示例将演示重载函数调用运算符的妙用：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Rect</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">int</span> width;</span><br><span class="line">    <span class="type">int</span> height;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Rect</span>() &#123;</span><br><span class="line">        width  = <span class="number">0</span>;</span><br><span class="line">        height = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">Rect</span>(<span class="type">int</span> a ,<span class="type">int</span> b) &#123;</span><br><span class="line">        width  = a;</span><br><span class="line">        height = b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">operator</span><span class="params">()</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;Area of myself is:&quot;</span> &lt;&lt; width * height &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Rect <span class="title">r1</span><span class="params">(<span class="number">3</span>, <span class="number">4</span>)</span>, <span class="title">r2</span><span class="params">(<span class="number">6</span>, <span class="number">8</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;r1: &quot;</span>; </span><br><span class="line">    <span class="built_in">r1</span>();</span><br><span class="line"></span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;r2: &quot;</span>;</span><br><span class="line">    <span class="built_in">r2</span>();</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译和运行以上示例，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">r1: Area of myself is:12</span><br><span class="line">r2: Area of myself is:48</span><br><span class="line">12</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="示例-8：下标运算符重载"><a href="#示例-8：下标运算符重载" class="headerlink" title="示例 8：下标运算符重载"></a>示例 8：下标运算符重载</h3><p>下标操作符（<code>[]</code>）通常用于访问数组元素。C++ 允许重载下标运算符用于增强操作 C++ 数组的功能，重载下标运算符最重要的作用就是设置一个哨兵，防止数组访问越界。</p>
<p>下面示例演示重载下标运算符：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="type">const</span> <span class="type">int</span> SIZE = <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Fibo</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="comment">// 偷懒，防止把 SIZE 设置的过小</span></span><br><span class="line">    <span class="type">int</span> arr[SIZE + <span class="number">3</span>];</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Fibo</span>() &#123;</span><br><span class="line">        arr[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">        arr[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">2</span>; i&lt;SIZE; i++) &#123;</span><br><span class="line">            arr[i] = arr[i<span class="number">-2</span>] + arr[i<span class="number">-1</span>];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="type">int</span>&amp; <span class="keyword">operator</span>[](<span class="type">unsigned</span> <span class="type">int</span> i) &#123;</span><br><span class="line">        <span class="keyword">if</span> (i &gt;= SIZE) &#123;</span><br><span class="line">            std::cout &lt;&lt; <span class="string">&quot;(索引超过最大值) &quot;</span>;</span><br><span class="line">            <span class="keyword">return</span> arr[<span class="number">0</span>]; <span class="comment">// 返回第一个元素</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> arr[i];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Fibo fb;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>; i&lt;SIZE+<span class="number">1</span>; i++) &#123;</span><br><span class="line">        cout &lt;&lt; fb[i] &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    cout &lt;&lt; endl;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译和运行以上示例，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">0 1 1 2 3 5 8 13 21 34 (索引超过最大值) 0</span><br><span class="line">1</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="示例-9：类成员访问运算符重载"><a href="#示例-9：类成员访问运算符重载" class="headerlink" title="示例 9：类成员访问运算符重载"></a>示例 9：类成员访问运算符重载</h3><p>C++ 允许重载类成员访问运算符（<code>-&gt;</code>），用于为一个类赋予 “指针” 行为。重载 <code>-&gt;</code> 运算符时需要注意以下几点：</p>
<ul>
<li>运算符 <code>-&gt;</code> 必须是一个成员函数；</li>
<li>如果使用了 <code>-&gt;</code> 运算符，返回类型必须是指针或者是类的对象；</li>
<li>运算符 <code>-&gt;</code> 通常与指针引用运算符 <code>*</code> 结合使用，用于实现智能指针的功能；</li>
<li>这些指针是行为与正常指针相似的对象，唯一不同的是，通过指针访问对象时，它们会执行其它的任务（比如，当指针销毁时，或者当指针指向另一个对象时，会自动删除对象）。</li>
</ul>
<p>间接引用运算符 <code>-&gt;</code> 可被定义为一个一元后缀运算符，比如：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Ptr</span>&#123;</span><br><span class="line">   <span class="comment">//...</span></span><br><span class="line">   X * <span class="keyword">operator</span>-&gt;();</span><br><span class="line">&#125;;</span><br><span class="line"><span class="number">1234</span></span><br></pre></td></tr></table></figure>

<p>类 <code>Ptr</code> 的对象可用于访问类 <code>X</code> 的成员，使用方式与指针的用法十分相似，如下：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">f</span><span class="params">(Ptr p )</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    p-&gt;m = <span class="number">10</span> ; <span class="comment">// (p.operator-&gt;())-&gt;m = 10</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>语句 <code>p-&gt;m</code> 被解释为 <code>(p.operator-&gt;())-&gt;m</code>。通过下面示例加深理解：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 假设一个实际的类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Obj</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="type">static</span> <span class="type">int</span> i, j;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">f</span><span class="params">()</span> <span class="type">const</span> </span>&#123; cout &lt;&lt; i++ &lt;&lt; endl; &#125;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">g</span><span class="params">()</span> <span class="type">const</span> </span>&#123; cout &lt;&lt; j++ &lt;&lt; endl; &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 静态成员定义</span></span><br><span class="line"><span class="type">int</span> Obj::i = <span class="number">10</span>;</span><br><span class="line"><span class="type">int</span> Obj::j = <span class="number">12</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 为上面的类实现一个容器</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ObjContainer</span></span><br><span class="line">&#123;</span><br><span class="line">    std::vector&lt;Obj*&gt; a;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">add</span><span class="params">(Obj* obj)</span> </span>&#123;</span><br><span class="line">        a.<span class="built_in">push_back</span>(obj);  <span class="comment">// 调用向量的标准方法</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="title class_">SmartPointer</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 实现智能指针，用于访问类 Obj 的成员</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">SmartPointer</span> &#123;</span><br><span class="line">    ObjContainer oc;</span><br><span class="line">    <span class="type">int</span> index;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">SmartPointer</span>(ObjContainer&amp; objc)</span><br><span class="line">    &#123;</span><br><span class="line">        oc = objc;</span><br><span class="line">        index = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 前缀版本</span></span><br><span class="line">    <span class="comment">// 返回值表示列表结束</span></span><br><span class="line">    <span class="type">bool</span> <span class="keyword">operator</span>++() </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span>(index &gt;= oc.a.<span class="built_in">size</span>())</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">if</span>(oc.a[++index] == <span class="number">0</span>)</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 后缀版本</span></span><br><span class="line">    <span class="type">bool</span> <span class="keyword">operator</span>++(<span class="type">int</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">operator</span>++();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 重载运算符 -&gt;</span></span><br><span class="line">    Obj* <span class="keyword">operator</span>-&gt;() <span class="type">const</span> </span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span>(!oc.a[index]) &#123;</span><br><span class="line">            std::cout &lt;&lt; <span class="string">&quot;Zero value&quot;</span>;</span><br><span class="line">            <span class="keyword">return</span> (Obj*)<span class="number">0</span>;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> oc.a[index];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">const</span> <span class="type">int</span> sz = <span class="number">6</span>;</span><br><span class="line"></span><br><span class="line">    Obj o[sz];</span><br><span class="line">    ObjContainer oc;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>; i&lt;sz; i++) &#123;</span><br><span class="line">        oc.<span class="built_in">add</span>(&amp;o[i]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function">SmartPointer <span class="title">sp</span><span class="params">(oc)</span></span>; </span><br><span class="line"></span><br><span class="line">    <span class="keyword">do</span> &#123;</span><br><span class="line">        sp-&gt;<span class="built_in">f</span>(); </span><br><span class="line">        sp-&gt;<span class="built_in">g</span>();</span><br><span class="line">    &#125; <span class="keyword">while</span>(sp++);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译和运行以上示例，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">10</span><br><span class="line">12</span><br><span class="line">11</span><br><span class="line">13</span><br><span class="line">12</span><br><span class="line">14</span><br><span class="line">13</span><br><span class="line">15</span><br><span class="line">14</span><br><span class="line">16</span><br><span class="line">15</span><br><span class="line">17</span><br><span class="line">123456789101112</span><br></pre></td></tr></table></figure>

<hr>
<h3 id="示例-10：逻辑非运算符重载"><a href="#示例-10：逻辑非运算符重载" class="headerlink" title="示例 10：逻辑非运算符重载"></a>示例 10：逻辑非运算符重载</h3><p>逻辑非（<code>!</code>）运算符是一元运算符，它总是出现在所操作对象的左边，如 <code>!obj</code>。逻辑非运算符运算符返回的类型为 <code>bool</code> 类型，当对象为真值时，返回假；当对象为假值时，返回真。</p>
<p>下面示例演示如何重载 <code>!</code> 运算符：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Rect</span></span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">int</span> width;</span><br><span class="line">    <span class="type">int</span> height;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="built_in">Rect</span>() &#123;</span><br><span class="line">        width = <span class="number">0</span>;</span><br><span class="line">        height = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">Rect</span>( <span class="type">int</span> a, <span class="type">int</span> b ) &#123;</span><br><span class="line">        width = a;</span><br><span class="line">        height = b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="type">int</span> <span class="title">area</span> <span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> width * height;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 当 width 或者 height 有一个小于 0 则返回 true</span></span><br><span class="line">    <span class="type">bool</span> <span class="keyword">operator</span>!() &#123;</span><br><span class="line">        <span class="keyword">if</span> ( width &lt;= <span class="number">0</span> || height &lt;= <span class="number">0</span> ) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="function">Rect <span class="title">r1</span><span class="params">(<span class="number">3</span>, <span class="number">4</span>)</span>, <span class="title">r2</span><span class="params">(<span class="number">-3</span>, <span class="number">4</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!r1) cout &lt;&lt; <span class="string">&quot;r1 is not a rectangle&quot;</span> &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">else</span> cout &lt;&lt; <span class="string">&quot;r1 is a rectangle&quot;</span> &lt;&lt; endl;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (!r2) cout &lt;&lt; <span class="string">&quot;r2 is not a rectangle&quot;</span> &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">else</span> cout &lt;&lt; <span class="string">&quot;r2 is a rectangle&quot;</span> &lt;&lt; endl;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>使用 <code>g++ main.cpp &amp;&amp; ./a.out</code> 命令编译和运行以上示例，输出结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">r1 is a rectangle</span><br><span class="line">r2 is not a rectangle</span><br></pre></td></tr></table></figure>

<h1 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h1><h2 id="1-传递参数"><a href="#1-传递参数" class="headerlink" title="1 传递参数"></a>1 传递参数</h2><h3 id="1-1-普通类型"><a href="#1-1-普通类型" class="headerlink" title="1.1 普通类型"></a>1.1 普通类型</h3><h4 id="传值参数"><a href="#传值参数" class="headerlink" title="传值参数"></a>传值参数</h4><p>简介：在值传递机制中，作为实参的表达式的值被复制到由对应的形参名所标识的对象中，成为形参的初值。完成参数值传递之后，函数体中的语句对形参的访问、修改都是在这个标识对象上操作的，与实参对象无关。</p>
<p>1）第一种格式，是在一开始就声明函数体，并赋予形参整形变量x,y，并在函数体里面对形参进行所需的运算，最后才写主函数，主函数中设置好实参a,b的值，再调用函数count将实参传回去计算。</p>
<blockquote>
<p>代码如下（示例）：</p>
</blockquote>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">count</span><span class="params">(<span class="type">int</span> x,<span class="type">int</span> y)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	x=x*<span class="number">2</span>;</span><br><span class="line">	y=y*y;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;x=&quot;</span>&lt;&lt;x&lt;&lt;<span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;y=&quot;</span>&lt;&lt;y&lt;&lt;endl;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> a=<span class="number">3</span>,b=<span class="number">4</span>;</span><br><span class="line">	<span class="built_in">count</span>(a,b);</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;a=&quot;</span>&lt;&lt;a&lt;&lt;<span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;y=&quot;</span>&lt;&lt;b&lt;&lt;endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
<p>2）第二种格式，是先写主函数，再写函数体。注意：这个时候会出现报错。为什么会报错？因为主函数找不到你所调用的函数在哪里。解决办法是：在主函数之前声明该函数即可。</p>
<blockquote>
<p>代码如下（示例）：</p>
</blockquote>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">count</span><span class="params">(<span class="type">int</span> x,<span class="type">int</span> y)</span></span>;<span class="comment">//函数声明</span></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> a=<span class="number">3</span>,b=<span class="number">4</span>;</span><br><span class="line">	<span class="built_in">count</span>(a,b);</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;a=&quot;</span>&lt;&lt;a&lt;&lt;<span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;y=&quot;</span>&lt;&lt;b&lt;&lt;endl;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">count</span><span class="params">(<span class="type">int</span> x,<span class="type">int</span> y)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	x=x*<span class="number">2</span>;</span><br><span class="line">	y=y*y;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;x=&quot;</span>&lt;&lt;x&lt;&lt;<span class="string">&#x27;\t&#x27;</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;y=&quot;</span>&lt;&lt;y&lt;&lt;endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/421be3c885054595ba486cc226dbe114.png" alt="在这里插入图片描述"></p>
<hr>
<h4 id="指针参数"><a href="#指针参数" class="headerlink" title="指针参数"></a>指针参数</h4><p>简介：当函数定义中的形参被说明为指针类型时，称为指针参数。形参指针对应的实参是地址表达式。调用函数时，实参把对象的地址赋给形参名标识的指针变量，被调用的函数可以在函数体内通过形参指针来间接访问实参地址所指的对象。这种参数传递方式称为指针传递或地址调用。</p>
<p>1）下面的例子中，x和y分别获取了a和b的地址，然后再通过swap函数进行交换。</p>
<blockquote>
<p>代码如下（示例）：</p>
</blockquote>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">swap</span><span class="params">(<span class="type">int</span> *x,<span class="type">int</span> *y)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> temp=*x;</span><br><span class="line">	*x=*y;</span><br><span class="line">	*y=temp;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> a=<span class="number">3</span>,b=<span class="number">8</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;before swaping.....\n&quot;</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;a=&quot;</span>&lt;&lt;a&lt;&lt;<span class="string">&quot;,b=&quot;</span>&lt;&lt;b&lt;&lt;endl;</span><br><span class="line">	<span class="built_in">swap</span>(&amp;a,&amp;b);</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;after swaping.....\n&quot;</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;a=&quot;</span>&lt;&lt;a&lt;&lt;<span class="string">&quot;,b=&quot;</span>&lt;&lt;b&lt;&lt;endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/af60849f15f746eea75b2a53a3eab812.png" alt="在这里插入图片描述"></p>
<p>2）如果我们不要指针那还会不会实现上面的交换功能呢？</p>
<blockquote>
<p>代码如下（示例）：</p>
</blockquote>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">swap</span><span class="params">(<span class="type">int</span> x,<span class="type">int</span> y)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> temp;</span><br><span class="line">	temp=x;</span><br><span class="line">	x=y;</span><br><span class="line">	y=temp;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> a=<span class="number">3</span>,b=<span class="number">8</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;before swaping.....\n&quot;</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;a=&quot;</span>&lt;&lt;a&lt;&lt;<span class="string">&quot;,b=&quot;</span>&lt;&lt;b&lt;&lt;endl;</span><br><span class="line">	<span class="built_in">swap</span>(a,b);</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;after swaping.....\n&quot;</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;a=&quot;</span>&lt;&lt;a&lt;&lt;<span class="string">&quot;,b=&quot;</span>&lt;&lt;b&lt;&lt;endl;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/dafb6b1295224ef3a272ae4402c84f27.png" alt="在这里插入图片描述"></p>
<p>显然它没有实现，为什么？因为它的本质是按值传递的，就是x和y获得了a,b的值，但是调用函数的时候a还是原来的值，b也是原来的值。这里形参x与实参的a其实是两个不同的存储空间，所以对于形参存储单元的操作，当然不会关联实参产生变化。并且我们的swap函数返回值类型是void，即ab交换后的值并没有被传递回来，所以在main函数中，ab的值仍然没有变。</p>
<hr>
<h4 id="引用参数"><a href="#引用参数" class="headerlink" title="引用参数"></a>引用参数</h4><p>简介：形参被定义为引用类型时被称为引用参数。引用参数对应的实参应该是对象名。函数被调用时，形参不需要开辟新的储存空间，形参名作为引用（别名）绑定于实参标识的对象上，执行函数体时，对形参的操作就是对实参对象的操作，直至函数执行结束，撤销引用绑定。</p>
<blockquote>
<p>代码如下（示例）：</p>
</blockquote>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">swap</span><span class="params">(<span class="type">int</span> &amp;,<span class="type">int</span> &amp;)</span></span>;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> a=<span class="number">3</span>,b=<span class="number">8</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;before swaping.....\n&quot;</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;a=&quot;</span>&lt;&lt;a&lt;&lt;<span class="string">&quot;,b=&quot;</span>&lt;&lt;b&lt;&lt;endl;</span><br><span class="line">	<span class="built_in">swap</span>(a,b);</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;after swaping.....\n&quot;</span>;</span><br><span class="line">	cout&lt;&lt;<span class="string">&quot;a=&quot;</span>&lt;&lt;a&lt;&lt;<span class="string">&quot;,b=&quot;</span>&lt;&lt;b&lt;&lt;endl;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">swap</span><span class="params">(<span class="type">int</span> &amp;x,<span class="type">int</span> &amp;y)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="type">int</span> temp=x;</span><br><span class="line">	x=y;</span><br><span class="line">	y=temp;</span><br><span class="line">&#125;</span><br><span class="line"> </span><br></pre></td></tr></table></figure>

<p>这里类似像取别名了，就是 int &amp;x&#x3D;a; int &amp;y&#x3D;b。</p>
<hr>
<h4 id="const参数"><a href="#const参数" class="headerlink" title="const参数"></a>const参数</h4><p>我们在设计函数时首先要考虑的就是函数用来干什么后，我们就可以考虑函数的具体实现了，<font color='red'>为了防止在实现函数的时候不能修改参数，我们可以使用const关键词修饰参数</font>。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="type">char</span> <span class="title">bl</span><span class="params">(<span class="type">const</span> string&amp; str)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//把下面一行设为注释就可以运行</span></span><br><span class="line">	str[str.<span class="built_in">length</span>() - <span class="number">1</span>] = <span class="string">&#x27;s&#x27;</span>;</span><br><span class="line">	<span class="keyword">return</span> str[str.<span class="built_in">length</span>() - <span class="number">1</span>];</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	string str = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;字符串最后一个字符为：&quot;</span> &lt;&lt; <span class="built_in">bl</span>(str) &lt;&lt; endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在本例中由于函数修改了字符串中的字符，违反了const语句，编译器就会报错。</p>
<h3 id="1-2-一维数组类型"><a href="#1-2-一维数组类型" class="headerlink" title="1.2 一维数组类型"></a>1.2 一维数组类型</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">changearr</span><span class="params">(<span class="type">int</span> a[],<span class="type">int</span> n)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">     cout&lt;&lt;<span class="built_in">sizeof</span>(a)&lt;&lt;endl;     <span class="comment">// 输出4</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">     <span class="type">int</span> a[<span class="number">10</span>] = &#123;<span class="number">2</span>,<span class="number">78</span>,<span class="number">100</span>,<span class="number">88</span>,<span class="number">12</span>,<span class="number">55</span>,<span class="number">45</span>,<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>&#125;;</span><br><span class="line">     cout&lt;&lt;<span class="built_in">sizeof</span>(a)&lt;&lt;endl;     <span class="comment">// 输出40</span></span><br><span class="line">     <span class="built_in">changearr</span>(a,<span class="number">10</span>);</span><br><span class="line">     <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在C++中，数组名就是指向数组首元素地址的指针，数组索引就是距离数组首元素地址的偏移量。</p>
<p>数组永远不会按值传递，它传递的是数组首元素的指针，即第0个元素的指针。</p>
<p>所以，数组做参数时，有以下两点需要明白的：</p>
<ol>
<li><p>在被调用函数内对参数数组的改变将被应用到数组实参上而不是本地拷贝上。</p>
</li>
<li><p>数组长度不是参数类型的一部分，函数不知道传递给它的数组的实际长度，编译器也不知道，当编译器</p>
</li>
</ol>
<p>  对实参类型进行参数类型检查时，并不会检查数组的长度。</p>
<p>以下三个函数声明是等价的：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">change_arr</span><span class="params">(<span class="type">int</span> a[])</span></span>;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">change_arr</span><span class="params">(<span class="type">int</span>* a)</span></span>;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">change_arr</span><span class="params">(<span class="type">int</span> a[<span class="number">10</span>])</span></span>;	<span class="comment">// 虽然标注是a[10],但是传入数组真实长度不会被检查,也就是说可能越界！</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果想让编译器检查数组的长度，你可以将数组参数声明为数组的引用：</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">change_arr</span><span class="params">(<span class="type">int</span> (&amp;a)[<span class="number">10</span>])</span></span>;</span><br></pre></td></tr></table></figure>

<h3 id="4-1-3-二维数组类型"><a href="#4-1-3-二维数组类型" class="headerlink" title="4.1.3 二维数组类型"></a>4.1.3 二维数组类型</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*传二维数组*/</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//第1种方式：传数组,第二维必须标明</span></span><br><span class="line"><span class="comment">/*void display(int arr[][4])*/</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">display1</span><span class="params">(<span class="type">int</span> arr[][<span class="number">4</span>],<span class="type">const</span> <span class="type">int</span> irows)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>;i&lt;irows;++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">0</span>;j&lt;<span class="number">4</span>;++j)</span><br><span class="line">        &#123;</span><br><span class="line">            cout&lt;&lt;arr[i][j]&lt;&lt;<span class="string">&quot; &quot;</span>;     <span class="comment">//可以采用parr[i][j]</span></span><br><span class="line">        &#125;</span><br><span class="line">        cout&lt;&lt;endl;</span><br><span class="line">    &#125;</span><br><span class="line">    cout&lt;&lt;endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//第2种方式：一重指针，传数组指针,第二维必须标明</span></span><br><span class="line"><span class="comment">/*void display(int (*parr)[4])*/</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">display2</span><span class="params">(<span class="type">int</span> (*parr)[<span class="number">4</span>],<span class="type">const</span> <span class="type">int</span> irows)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>;i&lt;irows;++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">0</span>;j&lt;<span class="number">4</span>;++j)</span><br><span class="line">        &#123;</span><br><span class="line">            cout&lt;&lt;parr[i][j]&lt;&lt;<span class="string">&quot; &quot;</span>;    <span class="comment">//可以采用parr[i][j]</span></span><br><span class="line">        &#125;</span><br><span class="line">        cout&lt;&lt;endl;</span><br><span class="line">    &#125;</span><br><span class="line">    cout&lt;&lt;endl;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//注意：parr[i]等价于*(parr+i)，一维数组和二维数组都适用</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//第3种方式：传指针,不管是几维数组都把他看成是指针</span></span><br><span class="line"><span class="comment">/*void display3(int *arr)*/</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">display3</span><span class="params">(<span class="type">int</span> *arr,<span class="type">const</span> <span class="type">int</span> irows,<span class="type">const</span> <span class="type">int</span> icols)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">for</span>(<span class="type">int</span> i=<span class="number">0</span>;i&lt;irows;++i)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">for</span>(<span class="type">int</span> j=<span class="number">0</span>;j&lt;icols;++j)</span><br><span class="line">        &#123;</span><br><span class="line">            cout&lt;&lt;*(arr+i*icols+j)&lt;&lt;<span class="string">&quot; &quot;</span>;   <span class="comment">//注意:(arr+i*icols+j),不是(arr+i*irows+j)</span></span><br><span class="line">        &#125;</span><br><span class="line">        cout&lt;&lt;endl;</span><br><span class="line">    &#125;</span><br><span class="line">    cout&lt;&lt;endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/***************************************************************************/</span></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">//第2种方式：一重指针，传数组指针void display(int (*parr)[4])</span></span><br><span class="line"><span class="comment">//缺陷：需要指出第二维大小</span></span><br><span class="line"><span class="comment">typedef int parr[4];</span></span><br><span class="line"><span class="comment">void display(parr *p)</span></span><br><span class="line"><span class="comment">&#123;</span></span><br><span class="line"><span class="comment">    int *q=*p;        //q指向arr的首元素</span></span><br><span class="line"><span class="comment">    cout&lt;&lt;*q&lt;&lt;endl;   //输出0</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">typedef int (*parr1)[4];</span></span><br><span class="line"><span class="comment">void display1(parr1 p)</span></span><br><span class="line"><span class="comment">&#123;</span></span><br><span class="line"><span class="comment">    cout&lt;&lt;(*p)[1]&lt;&lt;endl;  //输出1</span></span><br><span class="line"><span class="comment">    cout&lt;&lt;*p[1]&lt;&lt;endl;    //输出4,[]运算符优先级高</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">//第3种方式：</span></span><br><span class="line"><span class="comment">void display2(int **p)</span></span><br><span class="line"><span class="comment">&#123;</span></span><br><span class="line"><span class="comment">    cout&lt;&lt;*p&lt;&lt;endl;           //输出0</span></span><br><span class="line"><span class="comment">    cout&lt;&lt;*((int*)p+1+1)&lt;&lt;endl; //输出2</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="type">int</span> arr[][<span class="number">4</span>]=&#123;<span class="number">0</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>,<span class="number">11</span>&#125;;</span><br><span class="line">    <span class="type">int</span> irows=<span class="number">3</span>;</span><br><span class="line">    <span class="type">int</span> icols=<span class="number">4</span>;</span><br><span class="line">    <span class="built_in">display1</span>(arr,irows);</span><br><span class="line">    <span class="built_in">display2</span>(arr,irows);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//注意(int*)强制转换.个人理解：相当于将a拉成了一维数组处理。</span></span><br><span class="line">    <span class="built_in">display3</span>((<span class="type">int</span>*)arr,irows,icols);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="4-2-返回参数"><a href="#4-2-返回参数" class="headerlink" title="4.2 返回参数"></a>4.2 返回参数</h2><h3 id="4-2-1-返回数组指针"><a href="#4-2-1-返回数组指针" class="headerlink" title="4.2.1 返回数组指针"></a>4.2.1 返回数组指针</h3><blockquote>
<p>背景知识：</p>
<p>int arr[10]; &#x2F;&#x2F;arr是一个含有10个整数的<a target="_blank" rel="noopener" href="https://so.csdn.net/so/search?q=%E6%95%B0%E7%BB%84&spm=1001.2101.3001.7020">数组</a><br>int *p1[10]; &#x2F;&#x2F;p1是一个含有10个指针的数组<br>int (*p2)[10] &#x3D; &amp;arr; &#x2F;&#x2F;p2是一个数组指针，指向含有10个整数的数组。</p>
</blockquote>
<p>返回方式：定义类型别名</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">// arrT &lt;==&gt; int[10]</span></span><br><span class="line"><span class="keyword">typedef</span> <span class="type">int</span> arrT[<span class="number">10</span>]; </span><br><span class="line"></span><br><span class="line"><span class="function">arrT* <span class="title">getP</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> arrT[<span class="number">10</span>];</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="built_in">int</span>(*p)[<span class="number">10</span>] = <span class="built_in">getP</span>();</span><br><span class="line">    cout &lt;&lt; p[<span class="number">0</span>]; <span class="comment">// 这是一个未初始化的随机值</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="5-错题"><a href="#5-错题" class="headerlink" title="5 错题"></a>5 错题</h1><h2 id="5-1-找错"><a href="#5-1-找错" class="headerlink" title="5.1 找错"></a>5.1 找错</h2><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 错误1：没有包含头文件，无法使用 strcpy </span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">copystringandcount</span><span class="params">(<span class="type">char</span> *str)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">     <span class="type">int</span> count=<span class="number">0</span>;</span><br><span class="line">     <span class="type">char</span> *buffer;</span><br><span class="line">     buffer=<span class="keyword">new</span> <span class="type">char</span>[MAX_PATH_LENGTH];</span><br><span class="line">     <span class="built_in">strcpy</span>(buffer,str);</span><br><span class="line">     <span class="comment">// 错误2：buffer 应该修改为 *buffer，这样遍历到终止符&#x27;\0&#x27;时就会停止</span></span><br><span class="line">     <span class="keyword">for</span>(;buffer;buffer++)</span><br><span class="line">          <span class="comment">// 错误3：buffer 应该改为 *buffer</span></span><br><span class="line">          <span class="keyword">if</span>(buffer==<span class="string">&#x27;\\&#x27;</span>)count++;</span><br><span class="line">     <span class="comment">// 错误4：buffer动态分配空间，需要在函数返回前释放，使用 delete[] buffer ，使用数组分配</span></span><br><span class="line">     <span class="keyword">return</span> count;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>类似例题：</p>
<blockquote>
<p>找出下面代码中的错误：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">*<span class="built_in">mystrcat</span>(<span class="type">char</span> *dest, <span class="type">const</span> <span class="type">char</span> *src)</span><br><span class="line">&#123;</span><br><span class="line">        <span class="type">char</span> *temp = dest;</span><br><span class="line">        <span class="keyword">while</span>(*dest) dest++;</span><br><span class="line">        <span class="keyword">while</span>(*dest++ = *src++);</span><br><span class="line">        <span class="keyword">return</span> temp;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">        <span class="type">char</span> str1[<span class="number">100</span>] = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">        <span class="type">char</span> str2[<span class="number">100</span>] = <span class="string">&quot;world&quot;</span>;</span><br><span class="line">        <span class="built_in">mystrcat</span>(str1, str2);</span><br><span class="line">        cout &lt;&lt; str1 &lt;&lt; endl;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>代码的错误在于，<code>mystrcat()</code> 函数中没有对目标字符串 <code>dest</code> 的空间进行检查，可能会导致越界访问或者内存泄漏的问题。具体来说，当目标字符串的空间不够时，<code>mystrcat()</code> 函数会不断地将源字符串的字符复制到目标字符串中，直到遇到空字符 ‘\0’ 为止，这个过程中可能会超出目标字符串的空间。</p>
<p>修正的方法是，在 <code>mystrcat()</code> 函数中先判断目标字符串的空间是否够用，如果不够用则扩大目标字符串的空间。</p>
</blockquote>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="http://cjx_0723.gitee.io">Ther</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://cjx_0723.gitee.io/2023/03/18/%E5%90%8E%E7%AB%AF/C++%E5%9F%BA%E7%A1%80/C++%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91/">http://cjx_0723.gitee.io/2023/03/18/%E5%90%8E%E7%AB%AF/C++%E5%9F%BA%E7%A1%80/C++%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://cjx_0723.gitee.io" target="_blank">Ther的小站</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/C-%E5%9F%BA%E7%A1%80/">C++基础</a></div><div class="post_share"><div class="social-share" data-image="https://w.wallhaven.cc/full/1p/wallhaven-1ppld1.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/03/18/%E7%94%9F%E6%B4%BB/%E8%80%83%E7%A0%94%E5%A4%8D%E8%AF%95/%E9%9D%A2%E8%AF%95/" title="面试"><div class="cover" style="background: var(--default-bg-color)"></div><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">面试</div></div></a></div><div class="next-post pull-right"><a href="/2023/03/18/%E7%94%9F%E6%B4%BB/%E8%80%83%E7%A0%94%E5%A4%8D%E8%AF%95/%E4%B8%AD%E7%A7%91%E9%99%A2%E8%BD%AF%E4%BB%B6%E6%89%80%E5%A4%8D%E8%AF%95%E9%9D%A2%E8%AF%95%E3%80%81%E7%AC%94%E8%AF%95%E3%80%81%E5%8F%A3%E8%AF%95%E3%80%81%E6%9C%BA%E8%AF%95%E9%A2%98%E7%9B%AE%E6%95%B4%E7%90%86/" title="中科院软件所复试面试、笔试、口试、机试题目整理"><img class="cover" src="https://w.wallhaven.cc/full/e7/wallhaven-e7jj6r.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">中科院软件所复试面试、笔试、口试、机试题目整理</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/02/24/5O1day2nriDzjSu.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Ther</div><div class="author-info__description"></div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">31</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">21</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">7</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-%E5%8F%98%E9%87%8F%E7%9A%84%E9%BB%98%E8%AE%A4%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">1 变量的默认初始化</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#C-%E5%86%85%E7%BD%AE%E7%B1%BB%E5%9E%8B"><span class="toc-text">C++内置类型</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8C%85%E8%A3%85%E7%B1%BB%E5%9E%8B%EF%BC%88string%EF%BC%89"><span class="toc-text">包装类型（string）</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-text">关键字</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-sizeof"><span class="toc-text">1 sizeof</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8"><span class="toc-text">1.1 简单使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-sizeof%E8%AE%A1%E7%AE%97%E6%95%B0%E7%BB%84%E9%95%BF%E5%BA%A6"><span class="toc-text">1.2 sizeof计算数组长度</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-malloc"><span class="toc-text">2 malloc</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-malloc%E5%87%BD%E6%95%B0"><span class="toc-text">2.1 malloc函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-malloc%E5%87%BD%E6%95%B0%E4%B8%8E%E5%8A%A8%E6%80%81%E6%95%B0%E7%BB%84"><span class="toc-text">2.2 malloc函数与动态数组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-malloc%E5%87%BD%E6%95%B0%E4%B8%8E%E4%BA%8C%E7%BB%B4%E5%8A%A8%E6%80%81%E6%95%B0%E7%BB%84"><span class="toc-text">2.3 malloc函数与二维动态数组</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-define-%E4%B8%8E-typedef"><span class="toc-text">3 define 与 typedef</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-define"><span class="toc-text">3.1 define</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-typedef"><span class="toc-text">3.2 typedef</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-memset"><span class="toc-text">4 memset</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-memset%E4%BD%BF%E7%94%A8"><span class="toc-text">4.1 memset使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-memset%E4%B8%8E%E5%8A%A8%E6%80%81%E6%95%B0%E7%BB%84%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="toc-text">4.2 memset与动态数组初始化</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-union"><span class="toc-text">5 union</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%9F%A5%E8%AF%86%E7%82%B9"><span class="toc-text">知识点</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E5%A0%86%E5%92%8C%E6%A0%88"><span class="toc-text">1 堆和栈</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%94%B3%E8%AF%B7%E6%96%B9%E5%BC%8F"><span class="toc-text">申请方式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E9%85%8D%E6%96%B9%E5%BC%8F"><span class="toc-text">分配方式</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A9%BA%E9%97%B4%E5%A4%A7%E5%B0%8F"><span class="toc-text">空间大小</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%98%E5%82%A8%E5%86%85%E5%AE%B9"><span class="toc-text">存储内容</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-new-x2F-delete%E5%92%8Cmalloc-x2F-free"><span class="toc-text">2 new&#x2F;delete和malloc&#x2F;free</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-%E5%8C%BA%E5%88%AB"><span class="toc-text">2.1 区别</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-malloc%E7%9A%84%E5%86%85%E5%AD%98%E5%8F%AF%E4%BB%A5%E7%94%A8delete%E9%87%8A%E6%94%BE%E5%90%97%EF%BC%9F"><span class="toc-text">2.2 malloc的内存可以用delete释放吗？</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-text">3 运算符重载</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-1%EF%BC%9A%E4%B8%80%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-text">示例 1：一元运算符重载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-2%EF%BC%9A%E4%BA%8C%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-text">示例 2：二元运算符重载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-3%EF%BC%9A%E5%85%B3%E7%B3%BB%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-text">示例 3：关系运算符重载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-4%EF%BC%9A%E8%BE%93%E5%85%A5-x2F-%E8%BE%93%E5%87%BA%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-text">示例 4：输入&#x2F;输出运算符重载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-5%EF%BC%9A-%E5%92%8C-%E2%80%93-%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-text">示例 5：++ 和 – 运算符重载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-6%EF%BC%9A%E8%B5%8B%E5%80%BC%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-text">示例 6：赋值运算符重载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-7%EF%BC%9A%E5%87%BD%E6%95%B0%E8%B0%83%E7%94%A8%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-text">示例 7：函数调用运算符重载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-8%EF%BC%9A%E4%B8%8B%E6%A0%87%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-text">示例 8：下标运算符重载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-9%EF%BC%9A%E7%B1%BB%E6%88%90%E5%91%98%E8%AE%BF%E9%97%AE%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-text">示例 9：类成员访问运算符重载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B-10%EF%BC%9A%E9%80%BB%E8%BE%91%E9%9D%9E%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="toc-text">示例 10：逻辑非运算符重载</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%87%BD%E6%95%B0"><span class="toc-text">函数</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0"><span class="toc-text">1 传递参数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-%E6%99%AE%E9%80%9A%E7%B1%BB%E5%9E%8B"><span class="toc-text">1.1 普通类型</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BC%A0%E5%80%BC%E5%8F%82%E6%95%B0"><span class="toc-text">传值参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8C%87%E9%92%88%E5%8F%82%E6%95%B0"><span class="toc-text">指针参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%BC%95%E7%94%A8%E5%8F%82%E6%95%B0"><span class="toc-text">引用参数</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#const%E5%8F%82%E6%95%B0"><span class="toc-text">const参数</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84%E7%B1%BB%E5%9E%8B"><span class="toc-text">1.2 一维数组类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-1-3-%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E7%B1%BB%E5%9E%8B"><span class="toc-text">4.1.3 二维数组类型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E8%BF%94%E5%9B%9E%E5%8F%82%E6%95%B0"><span class="toc-text">4.2 返回参数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#4-2-1-%E8%BF%94%E5%9B%9E%E6%95%B0%E7%BB%84%E6%8C%87%E9%92%88"><span class="toc-text">4.2.1 返回数组指针</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-%E9%94%99%E9%A2%98"><span class="toc-text">5 错题</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1-%E6%89%BE%E9%94%99"><span class="toc-text">5.1 找错</span></a></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2023/09/06/%E7%94%9F%E6%B4%BB/%E5%AD%A6%E6%A0%A1/%E7%A0%94%E4%B8%80%E9%80%89%E8%AF%BE/" title="研一选课"><img src="https://w.wallhaven.cc/full/4g/wallhaven-4gwl6q.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="研一选课"/></a><div class="content"><a class="title" href="/2023/09/06/%E7%94%9F%E6%B4%BB/%E5%AD%A6%E6%A0%A1/%E7%A0%94%E4%B8%80%E9%80%89%E8%AF%BE/" title="研一选课">研一选课</a><time datetime="2023-09-06T07:55:08.667Z" title="发表于 2023-09-06 15:55:08">2023-09-06</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/07/26/%E5%90%8E%E7%AB%AF/%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F/MIT-6.824-%E5%88%86%E5%B8%83%E5%BC%8F%E7%B3%BB%E7%BB%9F%E8%AF%BE%E7%A8%8B%E7%AC%94%E8%AE%B0/" title="MIT-6.824-分布式系统课程笔记">MIT-6.824-分布式系统课程笔记</a><time datetime="2023-07-26T02:09:02.000Z" title="发表于 2023-07-26 10:09:02">2023-07-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/05/17/%E4%B8%AD%E9%97%B4%E4%BB%B6/mysql/MySQL%E6%95%99%E7%A8%8B%EF%BC%88%E4%B8%89%EF%BC%89%E2%80%94%E2%80%94%20MySQL%E9%AB%98%E7%BA%A7/" title="MySQL教程（三）—— MySQL高级"><img src="https://img1.baidu.com/it/u=950222251,316656728&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPG?w=967&amp;h=500" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="MySQL教程（三）—— MySQL高级"/></a><div class="content"><a class="title" href="/2023/05/17/%E4%B8%AD%E9%97%B4%E4%BB%B6/mysql/MySQL%E6%95%99%E7%A8%8B%EF%BC%88%E4%B8%89%EF%BC%89%E2%80%94%E2%80%94%20MySQL%E9%AB%98%E7%BA%A7/" title="MySQL教程（三）—— MySQL高级">MySQL教程（三）—— MySQL高级</a><time datetime="2023-05-17T12:12:30.479Z" title="发表于 2023-05-17 20:12:30">2023-05-17</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/05/17/%E5%89%8D%E6%B2%BF%E6%8A%80%E6%9C%AF/%E5%8C%BA%E5%9D%97%E9%93%BE/%E6%AF%95%E8%AE%BE/" title="区块链毕设">区块链毕设</a><time datetime="2023-05-17T08:40:58.540Z" title="发表于 2023-05-17 16:40:58">2023-05-17</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2023/05/15/%E4%B8%AD%E9%97%B4%E4%BB%B6/ElasticSearch/Linux%E7%B3%BB%E7%BB%9F%E4%B8%AD%E5%AE%89%E8%A3%85Elasticsearch%E5%B9%B6%E5%AE%9E%E7%8E%B0%E5%BC%80%E6%9C%BA%E8%87%AA%E5%8A%A8%E5%90%AF%E5%8A%A8/" title="Linux系统中安装Elasticsearch并实现开机自动启动"><img src="https://img1.baidu.com/it/u=950222251,316656728&amp;fm=253&amp;fmt=auto&amp;app=138&amp;f=JPG?w=967&amp;h=500" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Linux系统中安装Elasticsearch并实现开机自动启动"/></a><div class="content"><a class="title" href="/2023/05/15/%E4%B8%AD%E9%97%B4%E4%BB%B6/ElasticSearch/Linux%E7%B3%BB%E7%BB%9F%E4%B8%AD%E5%AE%89%E8%A3%85Elasticsearch%E5%B9%B6%E5%AE%9E%E7%8E%B0%E5%BC%80%E6%9C%BA%E8%87%AA%E5%8A%A8%E5%90%AF%E5%8A%A8/" title="Linux系统中安装Elasticsearch并实现开机自动启动">Linux系统中安装Elasticsearch并实现开机自动启动</a><time datetime="2023-05-15T08:26:31.492Z" title="发表于 2023-05-15 16:26:31">2023-05-15</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By Ther</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.1
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container').forEach(node => {
            if (node.hasAttribute('display')) {
              btf.wrap(node, 'div', { class: 'mathjax-overflow' })
            } else {
              btf.wrap(node, 'span', { class: 'mathjax-overflow' })
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typesetPromise()
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><div id="algolia-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="search-wrap"><div id="algolia-search-input"></div><hr/><div id="algolia-search-results"><div id="algolia-hits"></div><div id="algolia-pagination"></div><div id="algolia-info"><div class="algolia-stats"></div><div class="algolia-poweredBy"></div></div></div></div></div><div id="search-mask"></div><script src="https://cdn.jsdelivr.net/npm/algoliasearch/dist/algoliasearch-lite.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/instantsearch.js/dist/instantsearch.production.min.js"></script><script src="/js/search/algolia.js"></script></div></body></html>